﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OpenBlock.Utils
{
    public class Pool<T>
    {
        private List<T> recycledObjects = new List<T>();

        public T ObtainOrCreate(Func<T> createFunc)
        {
            if (recycledObjects.Count != 0)
            {
                var obj = recycledObjects.Last();
                recycledObjects.RemoveAt(recycledObjects.Count - 1);
                return obj;
            }
            else
            {
                return createFunc();
            }
        }

        public void Free(T obj)
        {
            recycledObjects.Add(obj);
        }
    }
}
